package com.simoniu.atm.service;

import com.simoniu.atm.domain.Account;
import com.simoniu.atm.util.DruidDBUtil;
import com.simoniu.util.DBUtil;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;

public class AccountService {

    public static Scanner sc = new Scanner(System.in);
    public static final String BANK_NO = "CCB"; //比方咱们开的都是建设银行的卡。

    //生成一个银行卡号。每个银行应用有一个固定银行编号。
    private String createAccountCid() {
        return BANK_NO+UUID.randomUUID().toString().replaceAll("-", "");
    }

    public Account createAccount() {

        String sql = "insert into account (cid,name,password,balance,modifydate) values (?,?,?,?,?)";
        List<Object> argsList = new ArrayList<Object>();
        String name;
        String password;

        try{
            System.out.println("请输入您的姓名:");
            name = sc.next();
            System.out.println("请设置您的银行卡密码：");
            password = sc.next();
            Account account = new Account();
            account.setCid(createAccountCid());
            account.setName(name);
            account.setPassword(password);
            account.setBalance(0);
            account.setModifydate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

            argsList.add(account.getCid());
            argsList.add(account.getName());
            argsList.add(account.getPassword());
            argsList.add(account.getBalance());
            argsList.add(account.getModifydate());

            //开启事务
            DruidDBUtil.startTransaction();
            DruidDBUtil.executeUpdate(sql, argsList);

            return account;
        }catch(Exception ex){
            ex.printStackTrace();
            DruidDBUtil.rollback();
            return null;
        }finally{

            DruidDBUtil.commit();
            DruidDBUtil.endTransaction();
            DruidDBUtil.release();
        }
    }

    public Account login(String cid,String password){
        String sql = "select * from account where cid=? and password=?;";
        List<Object> argsList = new ArrayList<Object>();
        try{
            argsList.add(cid);
            argsList.add(password);
            ResultSet rs = DruidDBUtil.executeQuery(sql,argsList);
            if(rs.next()){
                Account account = new Account();
                account.setCid(rs.getString("cid"));
                account.setName(rs.getString("name"));
                account.setPassword(rs.getString("password"));
                account.setBalance(rs.getDouble("balance"));
                account.setModifydate(rs.getString("modifydate"));
                return account;
            }
            return null;
        }catch(Exception ex){
            ex.printStackTrace();
            return null;
        }finally{
            DruidDBUtil.release();
        }
    }

    //存钱
    public boolean deposit(Account account, int money) {
        String sql = "update account set balance = balance + ? where cid = ?";
        List<Object> argsList = new ArrayList<Object>();
        argsList.add(money);
        argsList.add(account.getCid());

        try {
            DruidDBUtil.startTransaction();
            DruidDBUtil.executeUpdate(sql, argsList);

            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            DruidDBUtil.rollback();
            return false;
        } finally {
            DruidDBUtil.commit();
            DruidDBUtil.endTransaction();
            DruidDBUtil.release();

        }
    }

    //取款
    public boolean withdraw(Account account, int money) {

        int balance =  queryBalanceByCid(account.getCid());

        if(balance < money){
            System.out.println("余额不足！");
            return false;
        }

        String sql = "update account set balance = balance - ? where cid = ?";
        List<Object> argsList = new ArrayList<Object>();
        argsList.add(money);
        argsList.add(account.getCid());

        try {
            DruidDBUtil.startTransaction();
            DruidDBUtil.executeUpdate(sql, argsList);

            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            DruidDBUtil.rollback();
            return false;
        } finally {
            DruidDBUtil.commit();
            DruidDBUtil.endTransaction();
            DruidDBUtil.closeAll();
        }
    }

    //转账
    public boolean transfer(String sourceId, String destId, int number) {

        int balance = queryBalanceByCid(sourceId);
        if(balance<number){
            System.out.println("余额不足！");
            return false;
        }

        String sql = "update account set balance = balance + ? where cid = ?";
        List<List> argsList = new ArrayList<List>();
        try{
            DruidDBUtil.startTransaction();
            List list1 = new ArrayList();
            list1.add(number*-1);
            list1.add(sourceId);

            List list2 = new ArrayList();
            list2.add(number);
            list2.add(destId);

            argsList.add(list1);
            argsList.add(list2);
            DruidDBUtil.executeBatch(sql,argsList);

            return true;
        }catch(Exception ex){
            ex.printStackTrace();
            DruidDBUtil.rollback();

            return false;
        }finally{
            DruidDBUtil.commit();
            DruidDBUtil.endTransaction();
            DruidDBUtil.closeAll();
        }

    }

    //查询卡号是否存在
    public boolean checkAccountIdExists(String cid){
        ResultSet rs = null;
        String sql = "select * from account where cid=? ";
        List argsList = new ArrayList();
        argsList.add(cid);
        try {
            rs = DruidDBUtil.executeQuery(sql, argsList);
            if (rs.next()) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }


    //根据银行开户查询余额
    public int queryBalanceByCid(String cid) {
        ResultSet rs = null;
        String sql = "select balance from account where cid=? ";
        List<Object> argsList = new ArrayList<Object>();
        argsList.add(cid);
        try {
            rs = DruidDBUtil.executeQuery(sql, argsList);
            if (rs.next()) {
                return rs.getInt("balance");
            }
            return -1;
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }finally{
            DruidDBUtil.release();
        }
    }

}
